package com.hc.wdj.auth.server.processor;

import com.hc.wdj.auth.server.config.OAuthServerConfig;
import com.hc.wdj.auth.server.constant.GrantType;
import com.hc.wdj.auth.server.constant.OAuthConstants;
import com.hc.wdj.auth.server.constant.OAuthConstants.Api;
import com.hc.wdj.auth.server.constant.OAuthConstants.ResponseType;
import com.hc.wdj.auth.server.data.generate.OAuthDataGenerate;
import com.hc.wdj.auth.server.data.model.ClientModel;
import com.hc.wdj.auth.server.data.model.request.ClientIdAndSecretModel;
import com.hc.wdj.auth.server.error.OAuthError;
import com.hc.wdj.auth.server.manager.OAuthManager;
import com.hc.wdj.auth.server.template.OAuthTemplate;
import com.hc.wdj.auth.server.constant.OAuthConstants.Param;
import com.hc.wdj.core.context.HcHolder;
import com.hc.wdj.core.context.model.HcRequest;
import com.hc.wdj.core.context.model.HcResponse;
import com.hc.wdj.core.exception.HcException;

/**
 * @author ryanwdj
 * @since 2024/11/14
 */
public class OauthServerProcessor {
    /**
     * 全局默认实例
     */
    public static OauthServerProcessor instance = new OauthServerProcessor();

    public Object distributor(){
        HcRequest req = HcHolder.getRequest();
        //-------- 路由分发 ----------

        // 模式一： 授权码模式
        if(req.isPath(Api.authorize)){
            return authorize();
        }

        return OAuthConstants.NOT_HANDLE;
    }

    /**
     * 模式一：授权码模式
     * @return
     */
    public Object authorize(){
        // 获取变量
        HcRequest req = HcHolder.getRequest();
        HcResponse res = HcHolder.getResponse();
        OAuthServerConfig cfg = OAuthManager.getServerConfig();
        OAuthDataGenerate dataGenerate = OAuthManager.getDataGenerate();
        OAuthTemplate oAuthTemplate = OAuthManager.getTemplate();
        String responseType = req.getParamNotNull(Param.response_type);

        // 1、 判断是否开启了指定的授权模式
        checkAuthorizeResponseType(responseType, req, cfg);
        // 2、 todo:如果尚未登录，则先去登录
        return null;
    }

    /**
     * 校验 authorize 路由的 ResponseType 参数
     * @param responseType
     * @param req
     * @param cfg
     */
    public void checkAuthorizeResponseType(String responseType, HcRequest req, OAuthServerConfig cfg){
        // 模式一： Code授权码
        if(responseType.equals(ResponseType.code)){
            if(!cfg.enableAuthorizationCode){
                throw new HcException(OAuthError.AUTH_MODE_NOT_AVAILABLE);
            }
            if(!currClientModel().getAllowGrantTypes().contains(GrantType.client_credentials)){
                throw new HcException(OAuthError.AUTH_MODE_NOT_AVAILABLE);
            }
        }else {
            throw new HcException(OAuthError.INVALID_RESPONSE_TYPE, responseType);
        }
    }

    /**
     * 根据当前请求提交的client_id参数获取ClientModel对象
     *
     * @return
     */
    public ClientModel currClientModel(){
        OAuthTemplate oAuthTemplate = OAuthManager.getTemplate();
        ClientIdAndSecretModel clientIdAndSecret = OAuthManager.getDataResolver().readClientIdAndSecret(HcHolder.getRequest());
        return oAuthTemplate.checkClientModel(clientIdAndSecret.clientId);
    }

}
